package com.Brewing.springboot.controller.TpUsers;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.apache.yetus.audience.InterfaceAudience.Public;
import org.json.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.Brewing.springboot.common.BrewingJSONResult;
import com.Brewing.springboot.common.test.StringToJsonUtils;
import com.Brewing.springboot.common.test.StudentDataEntity;
import com.Brewing.springboot.controller.BasicController;
import com.Brewing.springboot.entity.TpUsers.TpUsers;
import com.Brewing.springboot.entity.TpUsersReport.TpUsersReport;
import com.Brewing.springboot.entity.vo.PublisherVideo;
import com.Brewing.springboot.entity.vo.TpUsersVO;
import com.Brewing.springboot.service.TpUsers.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Company: </p>
 * @author  庄磊
 * @date	2018年11月30日 下午4:51:51 
 * @version 1.0
 */
@RestController
@Api(value="Users-Api",tags= {"Users-Api"})
@RequestMapping("/user")
public class TpUsersController extends BasicController{
	
	@Autowired
	private UserService userService;
	
	@ApiOperation(value="用户上传头像", notes="用户上传头像的接口")
	@ApiImplicitParam(name="userId", value="用户id", required=true, dataType="String", paramType="query")
	@PostMapping("/uploadFace")
	public BrewingJSONResult uploadFace(String userId,
			@RequestParam("file") MultipartFile[] files) throws Exception {
		if(StringUtils.isBlank(userId)) {
			return BrewingJSONResult.errorMsg("用户id不能为空...");
		}
		//上传空间地址
		String fileSpace = FILE_SPACE;
		//本地
//		String fileSpace = "C:/Brewing-videos-dev";
		//服务器
//		String fileSpace = "/usr/local/Brewing-videos-dev";
		//保存到数据库的相对路径
		String uploadPathDB = "/"+ userId+"/face";
		FileOutputStream fileOutputStream =null;
		InputStream inputStream =null;
		try {
			if(files !=null && files.length>0) {
				String fileName = files[0].getOriginalFilename();
				if(StringUtils.isNotBlank(fileName)) {
					//文件上传的最终保存路径
					String finalFacePath = fileSpace+uploadPathDB+"/"+fileName;
					//设置数据库保存的路径
					uploadPathDB +=("/"+fileName);
					
					File outFile = new File(finalFacePath);
					if(outFile.getParentFile()!=null || outFile.getParentFile().isDirectory()) {
						//创建父文件夹
						outFile.getParentFile().mkdirs();
					}
					fileOutputStream=new FileOutputStream(outFile);
					inputStream = files[0].getInputStream();
					IOUtils.copy(inputStream, fileOutputStream);
				}
			}else {
				return BrewingJSONResult.errorMsg("上传出错...");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return BrewingJSONResult.errorMsg("上传出错...");
		} finally {
			if(fileOutputStream!=null) {
				fileOutputStream.flush();
				fileOutputStream.close();
			}
		}
		TpUsers tpUsers = new TpUsers();
		tpUsers.setId(userId);
		tpUsers.setFaceImage(uploadPathDB);
		userService.updateUserInfo(tpUsers);
		return BrewingJSONResult.ok(uploadPathDB);
	}
	@ApiOperation(value="查询用户信息", notes="查询用户信息的接口")
	@ApiImplicitParam(name="userId", value="用户id", required=true, dataType="String", paramType="query")
	@PostMapping("/query")
	public BrewingJSONResult query(String userId,String fanId) throws Exception{
		if(StringUtils.isBlank(userId)) {
			return BrewingJSONResult.errorMsg("用户id不能为空...");
		}
		TpUsers userInfo = userService.queryUserInfo(userId);
		TpUsersVO userVo = new TpUsersVO();
		BeanUtils.copyProperties(userInfo, userVo);
		
		userVo.setFollow(userService.queryIfFollow(userId,fanId));
		
		return BrewingJSONResult.ok(userVo);
	}
	
	@ApiOperation(value="查询用户信息视频页", notes="查询用户信息视频也的接口")
	@PostMapping("/queryPublisher")
	public BrewingJSONResult queryPublisher(String loginUserId,String videoId,String publishUserId) {
		if (StringUtils.isBlank(publishUserId)) {
			return BrewingJSONResult.errorMsg("");
		}
		// 1.查询视频发布者的信息
		TpUsers userInfo = userService.queryUserInfo(publishUserId);
		TpUsersVO publisher = new TpUsersVO();
		BeanUtils.copyProperties(userInfo, publisher);
		
		// 2.查询当前登陆者和视频的点赞关系
		boolean userLikeVideo = userService.isUserLikeVideo(loginUserId,videoId);
		PublisherVideo bean = new PublisherVideo();
		bean.setPublisher(publisher);
		bean.setUserLikeVideo(userLikeVideo);
		return BrewingJSONResult.ok(bean);
	}
	
	@ApiOperation(value="成为粉丝", notes="成为粉丝的接口")
	@PostMapping("/beyourfans")
	public BrewingJSONResult beyourfans(String userId,String fanId) {
		if(StringUtils.isBlank(userId) || StringUtils.isBlank(fanId)) {
			return BrewingJSONResult.errorMsg("");
		}
		userService.saveUserFanRelation(userId,fanId);
		return BrewingJSONResult.ok("关注成功...");
	}
	
	@ApiOperation(value="取消粉丝", notes="取消粉丝的接口")
	@PostMapping("/dontbeyourfans")
	public BrewingJSONResult dontbeyourfans(String userId,String fanId) {
		if(StringUtils.isBlank(userId) || StringUtils.isBlank(fanId)) {
			return BrewingJSONResult.errorMsg("");
		}
		userService.deleteUserFanRelation(userId,fanId);
		return BrewingJSONResult.ok("取消关注成功...");
	}
	
	@ApiOperation(value="举报用户", notes="举报用户的接口")
	@PostMapping("/reportUser")
	public BrewingJSONResult reportUser(@RequestBody TpUsersReport usersReport) {
		
		userService.reportUser(usersReport);
		
		return BrewingJSONResult.ok("举报成功...");
		
	}
	
	@ApiOperation(value="保存用户修改信息", notes="保存用户修改信息的接口")
	@PostMapping("/saveUserInfo")
	public BrewingJSONResult saveUser(String userId, String nickname) {
		userService.saveUserInfo(userId,nickname);
		return BrewingJSONResult.ok("修改用户信息");
	}
	@ApiOperation(value="testInfo",notes="testInfo的接口")
	@PostMapping("/testInfo")
	public JSONObject TestInfo() {
		StudentDataEntity studentDataEntity = new StudentDataEntity();
		return StringToJsonUtils.formatToJson(studentDataEntity);
	}
	
}
